﻿Imports System.Data.OleDb
Public Class UserProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
            Dim SqlString As String = "SELECT * FROM Profiles WHERE Use_Name=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("First_Name")) Then TB_FirstName.Text = dr("First_Name")
                If Not IsDBNull(dr("Last_Name")) Then TB_LastName.Text = dr("Last_Name")
                If Not IsDBNull(dr("U_DOB")) Then TB_DOB.Text = dr("U_DOB")
                If Not IsDBNull(dr("Use_Gender")) Then TB_Gender.Text = dr("Use_Gender")
                If Not IsDBNull(dr("Use_Desc")) Then TB_Description.Text = dr("Use_Desc")
                If Not IsDBNull(dr("Use_Email")) Then TB_Description.Text = dr("Use_Email")
            End If
        End If
    End Sub

    Protected Sub Bt_Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Bt_Submit.Click
        Dim newFileName As String = ""
        Dim SqlString As String
        If fup_RPicture.HasFile Then
            Dim myGUID = Guid.NewGuid()
            newFileName = myGUID.ToString() & ".jpg"
            Dim fileLocationOnServerHardDisk = Request.MapPath("Picture") & "\" & newFileName
            fup_RPicture.SaveAs(fileLocationOnServerHardDisk)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
        If fup_RPicture.HasFile Then
            SqlString = "UPDATE Profiles SET First_Name=@f1, Last_Name=@f2, U_DOB=@f3, Use_Gender=@f4,  Use_Desc=@f5, Use_Email=@f6, Use_Pic=@f7 WHERE Use_Name=@f8"
        Else
            SqlString = "UPDATE Profile SET First_Name=@f1, Last_Name=@f2, U_DOB=@f3, Use_Gender=@f4,  Use_Desc=@f5, Use_Email=@f6  WHERE Use_Name=@f8"
        End If


        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text


        cmd.Parameters.AddWithValue("@f1", TB_FirstName.Text)
        cmd.Parameters.AddWithValue("@f2", TB_LastName.Text)
        cmd.Parameters.AddWithValue("@f3", TB_DOB.Text)
        cmd.Parameters.AddWithValue("@f4", TB_Gender.Text)
        cmd.Parameters.AddWithValue("@f5", TB_Description.Text)
        cmd.Parameters.AddWithValue("@f6", Tb_emailid.Text)
        If fup_RPicture.HasFile Then cmd.Parameters.AddWithValue("@f7", "Picture/" & newFileName)
        cmd.Parameters.AddWithValue("@f8", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("ViewProfile.aspx?U_Name=" & User.Identity.Name)

    End Sub
End Class